/*
 *
 *  * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 *
 *
 */

package com.iwindplus.aider.web.listener;

import cn.hutool.core.bean.BeanUtil;
import com.iwindplus.base.operate.domain.dto.OperateLogDTO;
import com.iwindplus.base.operate.domain.event.OperateLogEvent;
import com.iwindplus.log.client.OperationLogClient;
import com.iwindplus.log.domain.dto.OperationLogDTO;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

/**
 * 操作日志监听器.
 *
 * @author zengdegui
 * @since 2025/03/21 21:51
 */
@Slf4j
public class OperateLogListener {

    @Autowired(required = false)
    private OperationLogClient operationLogClient;

    /**
     * 操作日志监听保存数据.
     *
     * @param operateLogEvent 日志事件
     */
    @Async
    @EventListener(OperateLogEvent.class)
    public void onApplicationEvent(OperateLogEvent operateLogEvent) {
        final OperateLogDTO logData = operateLogEvent.getOperateLogData();
        if (Objects.isNull(logData)) {
            return;
        }

        if (Objects.isNull(this.operationLogClient)) {
            return;
        }

        OperationLogDTO param = BeanUtil.copyProperties(logData, OperationLogDTO.class);
        this.operationLogClient.save(param);
    }
}
